<template>
  <div class="app-container">
    <el-form label-width="120px" >
      <el-form-item label="用户id" prop="name" >
        <el-input v-model="Like.userId"/>
      </el-form-item>

      <el-form-item label="评论id" prop="name" >
        <el-input v-model="Like.commentId"/>
      </el-form-item>

      <el-form-item>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="handeladd">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

import like from '../../api/like'

export default {
  data() {
    return {
      Like: {

        name: ''
      },
      saveBtnDisabled: false

    }
  },
  watch: {
    $route(to, from) {
      this.comment = {}
    }
  },
  created() {
    if (this.$route.params && this.$route.params.id) {
      const id = this.$route.params.id
      this.findAll(id)
    }
  },

  methods: {

    handeladd() {
      //   判断id是否存在，如果存在表示修改，如不存在表示新增
      if (this.$route.params && this.$route.params.id) {
        like.update(this.Like).then(res => {
          if (res.data != null) {
            this.$message.success('修改成功')
            this.$router.push({ path: this.redirect || '/like/list' })
          } else {
            this.$message.error('修改失败')
          }
        })
      } else {
        like.add(this.Like).then(res => {
          if (res.data !== false) {
            this.$message.success('新增成功')
            this.$router.push({ path: this.redirect || '/like/list' })
          } else {
            this.$message.error('新增失败,一个用户只能点赞一次')
          }
        })
      }
    },
    findAll(id) {
      like.findeByid(id).then(resp => {
        this.Like = resp.data
      })
    }
  }

}

</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
